<template>
  <div class="content-box">
    <div>加载中......</div>
  </div>
</template>

<script>
import { getPatientMsg, getDocInfo } from '@/api/dialog'
import { setTimeout } from 'timers';
import { stringify } from 'querystring';
export default {
  created() {
    this.query = this.$route.query
    this.model = this.$route.query.model || ''
    this.openid = this.$route.query.openid || ''
    // let userid = this.$route.query.userid || ''
    this.userid = this.$route.query.userid || ''
    // 将chatGroupId变成im可以识别的sessionId
    this.sessionId = this.$route.query.chatGroupId == 'null' || !this.$route.query.chatGroupId ? '' : ('team-' + this.$route.query.chatGroupId)
    sessionStorage.setItem('sessionId',this.sessionId)
    
    // 当进入的是用户填写信息页面的时候
    if (this.model === 'dsession') {
      if (this.userid) {
        this.getDoctorInfo(this.userid)
      } else {
        // 没有userid不能进行医生登录
        // 报错或者进入404页面
      }
    }else {
      if(this.openid){
        this.getPatientInfo(this.openid)
      }else{

      }
    }
  },
  data() {
    return {
      query:'',
      openid:'',
      model:'',
      userid:'',
      sessionId:'',
      doctorid:''
    }
  },
  methods: {
    getPatientInfo (openid) {
      getPatientMsg(openid).then(res => {
        let pUserInfo = res.data.data
        let imLoginInfo = {
          uid: pUserInfo.imUserId,
          sdktoken: pUserInfo.imUserToken
        }
        // 初始化sdk
        window.sessionStorage.userInfo = JSON.stringify(imLoginInfo)
        this.$store.dispatch('connect')

        // 就是判断是否连接sdk成功
        if(pUserInfo.imUserId) {
          if(this.model === 'pmsgsource' || this.model === 'psession') {
            if ((pUserInfo.relname || pUserInfo.age) && this.model === 'psession') {
              this.$router.push({path:'/dialog/pataintSession',query:{openid:this.openid}})
            }else{
              getDocInfo(this.userid).then( res => {
                  let doctorInfo = JSON.parse(JSON.stringify(res.data.data))
                  this.$router.push({path:'/dialog/parientmsgSource',query:{openid:this.openid,doctorid:doctorInfo.name}})
                  this.$store.dispatch('hideLoading')
                })
            }
          }else if(this.model === 'patientchat') {
            getDocInfo(this.userid).then( res => {
              let doctorInfo = JSON.parse(JSON.stringify(res.data.data))

              if(pUserInfo.relname || pUserInfo.age){
                if(this.sessionId) {
                  this.$router.push({path: `/patientChat/${this.sessionId}`,query:{openid:this.openid,userid:this.userid,pc:'pc'}})
                }else{
                  this.$router.push({path:'/dialog/pataintSession',query:{openid:this.openid}})
                }
              }else {
                this.$router.push({path:'/dialog/parientmsgSource',query:{openid:this.openid,doctorid:doctorInfo.name}})
              }
              this.$store.dispatch('hideLoading')
            })
          }
        }else{
          // 如果没有找到登录信息，可能是查询患者信息失败
          // 需要重新连接，查询患者信息
          this.$router.push({path:'/',query:this.query})
        }

      })
    },
    getDoctorInfo (userid) {
      getDocInfo(userid).then (res => {
        let dUserInfo = res.data.data
        let imLoginInfo = {
          uid: dUserInfo.imUserId,
          sdktoken: dUserInfo.imUserToken
        }

        // 初始化sdk
        window.sessionStorage.userInfo = JSON.stringify(imLoginInfo)
        this.$store.dispatch('connect')

        if (dUserInfo.imUserId) {
          var url = dUserInfo.roleType == 1 ? '/dialog/doctorSession' : '/dialog/doctorAidSession'
          this.$router.push({ path: url, query: { userid: this.userid }})
        } else {
           // 如果没有找到登录信息，可能是查询医生信息失败
          // 需要重新连接，查询医生信息
          this.$router.push({path:'/',query:this.query})
        }
        // }
      })
    }
  },
  components: {

  },
}
</script>

<style scoped lang="stylus">
</style>
